//
//  MyShopInfoTableViewController.m
//  CarTown
//
//  Created by 熊浩 on 15/9/3.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "MyShopInfoTableViewController.h"
#import "MyInfoBasicMode.h"
#import "Puser.h"
#import "MyDianpuMode.h"
#import <MJExtension.h>
#import "PhotoInfoMode.h"
#import "LogoPhotoTableViewCell.h"
#import "NoramlTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "PickerTableViewController.h"
#import "JianjieViewController.h"
#import "UserInfoUpdateResde.h"
#import "ShopMenMode.h"
#import "ZhanshiPhotoViewController.h"
#import "LoadingView.h"
#import "CatType.h"
#import "LogoChangeViewController.h"
@interface MyShopInfoTableViewController ()
@property (nonatomic,strong) Puser *user;
@property (nonatomic,strong) MyDianpuMode *dianpumode;
@property (nonatomic,strong) LoadingView *loading;
@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic,strong) PhotoInfoMode *mendian;
@end

@implementation MyShopInfoTableViewController
+(instancetype)viewcontroller{
    MyShopInfoTableViewController *vc=[[MyShopInfoTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
    return vc;
}
-(LoadingView *)loading{
    if (_loading==nil) {
        _loading=[LoadingView loading];
        _loading.frame=CGRectMake(0, 0, XHScreenwidth, XHScreenheight);
        _loading.hidden=YES;
    }
    return _loading;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([self.user.power integerValue]!=8) {
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(rightclick)];
    }
    self.navigationItem.title=@"我的店铺信息";
    
    self.view.backgroundColor=bgGarycolor;
    [self.view addSubview:self.loading];
    [self setupdata];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}
-(NSMutableArray *)array{
    if (_array==nil) {
        _array=[NSMutableArray array];
    }
    return _array;
}

-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}

-(void)setupdata{
    self.loading.hidden=NO;
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"c_shop_info";
    params[@"user_type"]=@"company";
    params[@"com_id"]=self.user.com_id;
    if ([self.user.power integerValue]==8) {
        params[@"parent_id"]=self.user.parent_id;
    }
    
    [PostUitls Post:userinfourl parameters:params success:^(id response) {
        NSDictionary *dict=response;
        NSNumber *state=dict[@"state"];
        if ([state integerValue]==400) {
            MyDianpuMode *dianpu=[MyDianpuMode objectWithKeyValues:dict[@"data"]];
            [self setupmode:dianpu];
        }
        
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)setupmode:(MyDianpuMode *)mode{
    MyInfoBasicMode *type=[[MyInfoBasicMode alloc]init];
    type.name=@"店铺类型";
    type.detail=mode.shop_categories;
    if ([self.user.power integerValue]!=8) {
        type.block=^{
            PickerTableViewController *piker=[PickerTableViewController picker];
            piker.currttext=type.detail;
            piker.block=^(NSString *str,NSInteger ct_id,NSInteger currtpostion){
                type.detail=str;
                
                NSIndexPath *indexpath=[NSIndexPath indexPathForRow:0 inSection:0];
                
                [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
            };
            [self.navigationController pushViewController:piker animated:YES];
        };

    }else{
        type.HideAccestory=YES;
    }
    MyInfoBasicMode *info=[[MyInfoBasicMode alloc]init];
    info.name=@"填写店铺简介";
    info.detail=[mode.shop_intro isEqualToString:@""]?@"未填写":@"已填写";
    if ([self.user.power integerValue]!=8) {
        info.block=^{
            JianjieViewController *jjvc=[JianjieViewController viewcontroller];
            jjvc.mode=self.user;
            jjvc.type=TYPEJIANJIE;
            jjvc.text=mode.shop_intro;
            jjvc.block=^(NSString *str){
                info.detail=@"已填写";
                NSIndexPath *indexpath=[NSIndexPath indexPathForRow:1 inSection:0];
                
                [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
                
            };
            [self.navigationController pushViewController:jjvc animated:YES];
        };
        
    }else{
        info.HideAccestory=YES;
    }
    
    
    PhotoInfoMode *logo=[[PhotoInfoMode alloc]init];
    logo.name=@"门头logo照片";
    logo.imgstr=self.user.shop_logo;
    NSLog(@"%@",self.user.shop_logo);
    logo.block=^{
        [self pustologochange];
    };
    
    
    PhotoInfoMode *xuan=[[PhotoInfoMode alloc]init];
    xuan.name=@"门店宣传照片";
    xuan.imgstr=mode.xuan;
    xuan.block=^{
        [self getphotoarray];
    };
    self.mendian=xuan;
    [self.array addObject:type];
    [self.array addObject:info];
    [self.array addObject:logo];
    [self.array addObject:xuan];
    self.loading.hidden=YES;
    [self.tableView reloadData];
}
-(void)pustologochange{
    LogoChangeViewController *logovc=[LogoChangeViewController viewcontroller];
    logovc.block=^{
        [self.tableView reloadData];
    };
    [self.navigationController pushViewController:logovc animated:YES];
}
-(void)getphotoarray{
     ZhanshiPhotoViewController *zspvc=[ZhanshiPhotoViewController viewcontroller];
    if (self.user.shop_men_imgs) {
        NSArray *array=[NSString JsontoData:self.user.shop_men_imgs];
         NSMutableArray *photoarray=[NSMutableArray arrayWithArray:array];
        zspvc.photoaray=photoarray;
    }else{
        zspvc.photoaray=[NSMutableArray array];
    }
    
    zspvc.title=@"门店照片";
    zspvc.titlelable.text=@"门店照片";
    zspvc.block=^(NSString *shop_men_img1){
        self.mendian.imgstr=shop_men_img1;
        [self reloadtableView:0 row:3];
        
    };
    [self.navigationController pushViewController:zspvc animated:YES];
}

-(void)rightclick{
     MyInfoBasicMode *type=self.array[0];
    if ([type.detail isEqualToString:self.dianpumode.shop_categories]) {
        return;
    }
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.labelText=@"正在保存";
   
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"c_shop_change";
    params[@"user_type"]=@"company";
    
    if (self.user.parent_id&&![self.user.parent_id isEqualToString:@"0"]) {
        params[@"com_id"]=self.user.parent_id;
    }else{
        params[@"com_id"]=self.user.com_id;
    }
   CatType *cattype=[CatType getCatTypeBytext:type.detail];
    
    params[@"cate"]=cattype.cat_id;
    [PostUitls Post:userinfourl parameters:params success:^(id response) {
        hud.mode=MBProgressHUDModeText;
        UserInfoUpdateResde *resde=[UserInfoUpdateResde resdeWithDict:response];
        if ([resde.state integerValue]==400) {
            hud.labelText=@"保存成功";
        }else{
            hud.labelText=resde.msg;
        }
        [hud hide:YES afterDelay:1];
    } failure:^(NSError *error) {
        hud.labelText=@"网络异常";
         hud.mode=MBProgressHUDModeText;
         [hud hide:YES afterDelay:1];
    }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
-(void)reloadtableView:(NSInteger)section row:(NSInteger)row{
    NSIndexPath *indexpath=[NSIndexPath indexPathForRow:row inSection:section];
    
    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyInfoBasicMode *mode=self.array[indexPath.row];
    if ([mode isKindOfClass:[PhotoInfoMode class]]) {
        PhotoInfoMode *photomode=mode;
        
        LogoPhotoTableViewCell *logocell=[LogoPhotoTableViewCell cellWithTableView:tableView];
        logocell.title.text=photomode.name;
        if (![photomode.imgstr isEqualToString:@""]&&photomode.imgstr) {
            logocell.photo.hidden=NO;
            [logocell.photo sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:photomode.imgstr]] placeholderImage:[UIImage imageNamed:@"placehoder"]];
        }else{
            logocell.photo.hidden=YES;
        }
        return logocell;
    }else{
        NoramlTableViewCell *cell=[NoramlTableViewCell cellWithTableView:tableView];
        cell.textLabel.text=mode.name;
        cell.detailTextLabel.text=mode.detail;
        if (mode.HideAccestory) {
             cell.accessoryType=UITableViewCellAccessoryNone;
        }else{
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;}
         return cell;
    }
    // Configure the cell..
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInfoBasicMode *mode=self.array[indexPath.row];
    if ([mode isKindOfClass:[PhotoInfoMode class]]) {
        return 60;
    }else{
        return 44;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInfoBasicMode *mode=self.array[indexPath.row];
    if (mode.block) {
        mode.block();
    }
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
